'use strict';

const db = uniCloud.database();
const collection = db.collection('blogs');
exports.main = async (event, context) => {
	const {
		userName,
		content,
		blogId
	} = event;

	// 找到这篇文章，在其评论信息上添加其新的评论信息
	const res = await collection.doc(blogId).get();
	let arr = [];
	if (res.data.comments) {
		arr = res.data.comments;
	}
	arr.push({
		userName,
		content
	});
	const res2 = await collection.doc(blogId).update({ comments: arr });


	//返回数据给客户端
	return {
		code: 0,
		data: res2.data,
		msg: '评论成功'
	};
};